<template>
	<view class="house">
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar leftText="功能反馈" :autoBack="true">
			</u-navbar>
		</view>
		<view class="con-body" style="padding-left: 20rpx; padding-right: 20rpx; background-color: #F8F8F8;">
			<!-- 功能反馈 -->
			<view class="uni-flex-center">
				<view class="main-service uni-cell-90">
					<view class="user-list" style="padding:20rpx 0;">
						<view style="padding-left: 30rpx; padding-right: 30rpx; padding-bottom: 10rpx;">
						<view style="padding-top:20rpx;">建议内容</view>
							<u-textarea v-model="content" :border="false" placeholder="请详细描述您的建议"></u-textarea>
							<u-line></u-line>
						</view>
						<view style="margin-top: 20rpx;margin-left: 40rpx;">
							<view style="padding-bottom:20rpx;">图片反馈</view>
							<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
								:maxCount="9"></u-upload>
						</view>
					</view>
				</view>
				<view style="padding:20rpx 0;font-size: 24rpx;color:#126970;">注：欢迎您的反馈，请如实反馈情况，尽可能的详细描述！</view>
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>
		<u-button :customStyle="btnStyle" @click="report">提交</u-button>
	</view>
</template>

<script>
	import {
		report
	} from "@/service/userService.js";
	export default {
		data() {
			return {
				btnStyle: {
					width: '96%',
					borderRadius: '40rpx',
					marginLeft: '2%',
					backgroundImage: "url('https://resourse.cnlhjt.com/upload/20220825/d1aa35c1d129137d9b12c8f34e59c5bc.png')",
					backgroundSize: '100% 80rpx',
					color: '#FFFFFF',

					position: 'fixed',
					bottom: '80rpx'
				},
				content: '',
				fileList1: [],
				images: []
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.images.splice(event.index,1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://admin.gslhzhwy.com/api/upload/file', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'smart-property'
						},
						success: (res) => {
							let par = JSON.parse(res.data);
							if (par.code == 1) {
								this.images.push(par.data.url);
							}
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			report() {
				report({
					content: this.content,
					images: this.images
				}).then(res => {
					if (res.code == 200) {
						this.$refs.uToast.show({

							type: 'success',
							title: '成功',
							message: res.msg,
							// complete() {
							// 	uni.navigateBack()
							// }
						})
						setTimeout(function(){
							uni.navigateBack({
								delta: 1
							})
						},300)
						
					} else {
						this.$refs.uToast.show({

							type: 'error',
							title: '失败',
							message: res.msg,
							complete() {
								uni.navigateBack({
									delta: 1
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>
